<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/24
 * Time: 20:25
 */

namespace Admin\Controller;

/**
 * Class VideoController
 * @package Admin\Controller
 */
class VideoController extends AdminController
{
    /**
     * @var \Think\Model $m
     */
    private $m;

    function _initialize()
    {
        parent::_initialize();
        $this->m = D('video');
    }

    function index()
    {
        $where = array();
        if (I('get.keyword')) $where['title'] = array('like', '%' . I('get.keyword') . '%');
        $list = $this->lists($this->m, $where);
        $this->assign('meta_title','发修视频');
        $this->assign("list", $list);
        $this->display();
    }

    function add()
    {
        $this->assign('meta_title','添加视频');
        $this->display();
    }

    function edit($id)
    {
        $info = $this->m->find($id);
        $this->assign('meta_title','编辑视频');
        $this->assign('info', $info);
        $this->display('add');
    }

    function save()
    {
        if (IS_POST) {
            $data['url'] = I('post.url');
            $data['title'] = I('post.title');
            $data['play_num'] = I('post.play_num');
            $data['pic'] = I('post.pic',0);
            $data['id'] = I('post.id', 0);
            if ($data['url'] == '') {
                $this->error('请填写视频连接');
            } elseif ($data['title'] == '') {
                $this->error('请填写视频标题');
            }
            if ($data['id']) {
                $result = $this->m->save($data, array('id' => $data['id']));
            } else {
                $result = $this->m->add($data);
            }
            $result !== false ? $this->success('保存成功', U('index')) : $this->error('保存失败');
        }
    }

    function delete($id)
    {
        $id = array_unique((array)I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
        if ($this->m->where($map)->delete()) {
            // S('DB_CONFIG_DATA',null);
            //记录行为
            action_log('delete_store', 'Video', $id, UID);
            $this->success('删除成功', U('index'));
        } else {
            $this->error('删除失败！');
        }
    }
}